(function(angular) {
	'docTemplate strict';
	angular.module('fs.waf').controller('cda.docTemplate.updateVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'docTemplateService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, docTemplateService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.docTemplate = {};
		ctrl.docTemplateOld = {};
		
		ctrl.init = function() {
			//init dTempParms  
			ctrl.innerPreGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPreParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPreGridOptions.data = 'vm.docTemplate.docTempPreParms';
			ctrl.innerPostGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.docTempPostParms.tableConfigs.table, rowSelectionChanged);
			ctrl.innerPostGridOptions.data = 'vm.docTemplate.docTempPostParms';
			ctrl.innerPreGridOptions.enableRowHeaderSelection = false;
			//postParms
			ctrl.innerPostGridOptions.enableRowHeaderSelection = false;

			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			// init docTemplate Parameter
			docTemplateService.initDataOptions($scope, ctrl);
			
			authService.updateVerify(params).then(function(response) {
				ctrl.docTemplate = response.data.model.newModel;
				ctrl.docTemplateOld = response.data.model.oldModel;
				ctrl.docTemplate.taskId = response.data.task.taskId;
				ctrl.docTemplate.docTempPreParms = ctrl.docTemplate.docTempPreParms || [];
				ctrl.docTemplate.docTempPostParms = ctrl.docTemplate.docTempPostParms || [];
				
			});
		};
		
		
		ctrl.updateVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.docTemplate.comments = comments;
						authService.updateVerifyReject(ctrl.docTemplate).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.updateVerifyApprove = function() {
			docTemplateService.setPrePostParm($scope, ctrl);
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.docTemplate.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.updateVerifyApprove(ctrl.docTemplate).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.doctemplate.list',
					'state' : 'cda.docTemplate.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.doctemplate.list',
					'state' : 'cda.docTemplate.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);